import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {LoadingServer} from '../../common/loading/loading.server';
import {NzMessageService} from 'ng-zorro-antd';
import {JM_LOGIN_PATH} from '../../config/config';

/**
 * @author leihfei
 * @date 2019-05-13
 * @description 是否登录路由拦截
 */
@Injectable()
export class LoginActivate implements CanActivate {
  constructor(private loading: LoadingServer, private route: Router, private message: NzMessageService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    // 实现逻辑，检查是否进行了登录即可，直接利用loadingService这个业务服务
    if (this.loading.youLogin()) {
      if (this.loading.checkRoute(state.url)) {
        return true;
      } else {
        this.message.warning('您无权限访问该页面!');
        return false;
      }
      return true;
    }
    this.message.warning('您未登录，请登录后重试!');
    // this.route.navigateByUrl('/login');
    window.open(JM_LOGIN_PATH, '_self');
    return false;
  }


}
